package com.wzy.es.controller;

import com.wzy.es.entity.Movie;
import com.wzy.es.repository.MoviesRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/movies")
public class MovieController {
    private final MoviesRepository moviesRepository;

    public MovieController(MoviesRepository moviesRepository) {
        this.moviesRepository = moviesRepository;
    }

    //    @GetMapping
//    public List<Movie> getPageData(@RequestParam("genre") String genre,
//                                   @RequestParam(defaultValue = "0") Integer page,
//                                   @RequestParam(defaultValue = "10") Integer size) {
//        Pageable pageable = PageRequest.of(page, size);
//        Page<Movie> pageData = moviesRepository.findByGenre(genre, pageable);
//        return pageData.getContent();
//    }
    @GetMapping
    public List<Movie> getPageData(@RequestParam("title") String title,
                                   @RequestParam(defaultValue = "0") Integer page,
                                   @RequestParam(defaultValue = "10") Integer size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Movie> pageData = moviesRepository.findByTitle(title, pageable);
        return pageData.getContent();
    }

    @GetMapping("/multi")
    public List<Movie> getPageMulti(@RequestParam("title") String title,
                                    @RequestParam("genre") String genre,
                                    @RequestParam(defaultValue = "0") Integer page,
                                    @RequestParam(defaultValue = "10") Integer size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Movie> byTitleAndGenre = moviesRepository.findByTitleAndGenre(title, genre, pageable);
        return byTitleAndGenre.getContent();
    }

    @GetMapping("/between")
    public List<Movie> getPageBetween(@RequestParam("title") String title,
                                      long begin,
                                      long end,
                                      @RequestParam(defaultValue = "0") Integer page,
                                      @RequestParam(defaultValue = "10") Integer size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Movie> data = moviesRepository.findByTitleAndYearBetween(title, begin, end, pageable);
        return data.getContent();
    }

    @GetMapping("/my")
    public List<Movie> get(String title, String genre) {
        List<Movie> movies = moviesRepository.get(title, genre);
        return movies;
    }
}
